package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CourseExcelData;
import com.ruoyi.system.domain.CoursePlan;
import com.ruoyi.system.utils.Upload;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证码操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/test")
@Slf4j
public class TestExportController extends BaseController {

    private CoursePlan read() throws IOException {

        CoursePlan coursePlan = new CoursePlan();
        FileInputStream file = new FileInputStream("/Users/timmy/work/school/ruoyi-ui/public/课程表模板.xlsx");

        XSSFWorkbook wb1 = new XSSFWorkbook(file);

        String[][] result = new String[0][];
        try {
            // 获取Excel表文件里的数据
            //第二个参数为从Excel文件里第几行开始读
            result = Upload.getFullData(wb1, 0);

            for (int i = 0; i < result.length; i++) {
                for (int j = 0; j < result[i].length - 1; j++) {
                    log.info("result[i][j] = " + "result[" + i + "][" + j + "] = " + result[i][j]);
                }
                if (i == 0) {
                    String yq = result[i][0];
                    String pattern = "([1-9]\\d*)|([零|一|二|三|四|五|六|七|八|九|十]+)";

                    Pattern r = Pattern.compile(pattern);
                    Matcher m = r.matcher(yq);
                    if (m.groupCount() == 2) {
                        m.find();
                        coursePlan.setYear(m.group());
                        m.find();
                        coursePlan.setQuarter(m.group());
                    }
                }
                if (i == 1) {
                    coursePlan.setGrade(result[i][1]);
                    coursePlan.setClazz(result[i][3]);
                }

                if (i > 2) {

                    coursePlan.getPeriod().add(result[i][0]);
                    coursePlan.getId().add(result[i][1]);
                    coursePlan.getMonday().add(result[i][2]);
                    coursePlan.getTuesday().add(result[i][3]);
                    coursePlan.getWednesday().add(result[i][4]);
                    coursePlan.getThursday().add(result[i][5]);
                    coursePlan.getFriday().add(result[i][6]);
                    coursePlan.getSaturday().add(result[i][7]);
                    coursePlan.getSunday().add(result[i][8]);
                }
            }
            file.close();

        } catch (Exception e) {
            log.error("导入失败", e);
        } finally {
            file.close();
        }
        return coursePlan;
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<CourseExcelData> list = Lists.newArrayList();
        CoursePlan plan = read();
        for (int i = 0; i < plan.getPeriod().size(); i++) {
            CourseExcelData c = getCourseExcelData(plan, i);
            list.add(c);
        }

        ExcelUtil<CourseExcelData> util = new ExcelUtil<>(CourseExcelData.class);
        util.exportExcel(response, list, "课程表数据", plan.getYear() + "学年度第" + plan.getQuarter() + "学期 " + plan.getGrade() +
                " " + plan.getClazz() + " 课程表");
    }

    private static CourseExcelData getCourseExcelData(CoursePlan plan, int i) {
        CourseExcelData c = new CourseExcelData();
        c.setPeriod(plan.getPeriod().get(i));
        c.setId(plan.getId().get(i));
        c.setMonday(plan.getMonday().get(i));
        c.setTuesday(plan.getTuesday().get(i));
        c.setWednesday(plan.getWednesday().get(i));
        c.setThursday(plan.getThursday().get(i));
        c.setFriday(plan.getFriday().get(i));
        c.setSaturday(plan.getSaturday().get(i));
        c.setSunday(plan.getSunday().get(i));
        return c;
    }
}
